//
//  XMStautsCell.swift
//  XMWeimo
//
//  Created by ios001 on 16/7/23.
//  Copyright © 2016年 itcast. All rights reserved.
//

import UIKit
import SnapKit //需要导入这个,才能使用Constraint属性
// cell中使用的间距
let statusCellMargin: CGFloat = 10
class XMStautsCell: UITableViewCell {
    
    // 记录底部视图的顶部约束
    var toolBarBottomconstraint:Constraint?
    
    // 定义ViewModel
    var statusViewModel: XMStatusViewModel?{
        didSet{
            // 给原创微博赋值
            originalView.statusViewModel = statusViewModel
            
            self.toolBarBottomconstraint?.uninstall()
            // 没有转发微博
            if statusViewModel?.status?.retweeted_status == nil {
                toolBarView.snp_updateConstraints(closure: { (make) -> Void in
                    self.toolBarBottomconstraint = make.top.equalTo(originalView.snp_bottom).constraint
                })
                // 隐藏
                retweetView.hidden = true
            }else{
                // 记住先赋值
                retweetView.statusViewModel = statusViewModel
                // 有转发微博
                toolBarView.snp_updateConstraints(closure: { (make) -> Void in
                    self.toolBarBottomconstraint = make.top.equalTo(retweetView.snp_bottom).constraint
                })
                // 显示
                retweetView.hidden = false
            }
            
            // 给底部视图赋值
            toolBarView.statusViewModel = statusViewModel
        }
    }

    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    // MARK: - 设置视图
    private func setupUI(){
        backgroundColor = RGB(237, green: 237, blue: 237)
        // 添加控件
        contentView.addSubview(originalView)
        contentView.addSubview(toolBarView)
        contentView.addSubview(retweetView)
        // 添加约束
        originalView.snp_makeConstraints { (make) -> Void in
//            make.leading.top.trailing.equalTo(contentView)
            make.leading.trailing.equalTo(contentView)
            make.top.equalTo(contentView).offset(8)
            // 当前约束已经在当前原创微博中约束
        }
        
        retweetView.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(originalView.snp_bottom)
            make.leading.trailing.equalTo(contentView)
        }
        
        toolBarView.snp_makeConstraints { (make) -> Void in
            self.toolBarBottomconstraint = make.top.equalTo(retweetView.snp_bottom).constraint
            make.leading.trailing.equalTo(contentView)
            make.height.equalTo(35)
        }
        // 约束contentView
        contentView.snp_makeConstraints { (make) -> Void in
            make.leading.trailing.top.equalTo(self)
            make.bottom.equalTo(toolBarView)
        }
    }
    // MARK: - 懒加载控件
    // 原创微博
    private lazy var originalView: XMStatusOriginalView = XMStatusOriginalView()
    // 底部视图
    private lazy var toolBarView: XMStatusToolBarView = XMStatusToolBarView()
    // 转发微博
    private lazy var retweetView: XMStatusRetwwtView = XMStatusRetwwtView()
}
